// 问卷调查
import React, { useReducer, useState } from "react";
import queryApi from "../../api/query";

const userQuesContext = React.createContext();

let newData = {
  target: -1, // 0：一般户
  title: "",
  body: [],
  state: 0,
  time: "",
  user: {},
  image: [],
  video: [],
  soundfile: [],
  userproblem: {},
  opinions: {},
};

const dataReducer = (state, action) => {
  switch (action.type) {
    case "new_ques":
      return {
        ...newData,
      };
    case "update_answer":
      return {
        _id: state._id,
        title: state.title,
        target: state.target,
        body: action.payload.body,
      };
    case "update_ques":
      return action.payload.newQues;
    case "get_ques":
      newData = {
        ...newData,
        target: action.payload.ques.target,
        title: action.payload.ques.title,
        body: action.payload.ques.body,
      };
      return {
        ...newData,
      };
    default:
      return state;
  }
};

let initData = newData;

export const UserQuesProvider = ({ children }) => {
  const [UserQuesData, dispatch] = useReducer(dataReducer, initData);
  const [useQuesLoading, setUserQuesLoading] = useState("end");

  const newQues = async (index) => {
    setUserQuesLoading("ing");
    await dispatch({ type: "new_ques", payload: { index } });
    setUserQuesLoading("end");
    // console.log("新建问卷啦---", newData);
  };
  const updateAnswer = (body) => {
    dispatch({ type: "update_answer", payload: { body } });
    // console.log("更新答案啦---", UserQuesData);
  };
  const updateQues = (newQues) => {
    dispatch({ type: "update_ques", payload: { newQues } });
    // console.log("更新题目啦---", UserQuesData);
  };

  const getQuesTemplate = async () => {
    try {
      setUserQuesLoading("ing");
      let data = await queryApi.get("ques");
      console.log("查找问卷模板成功---", data.data.status);
      dispatch({ type: "get_ques", payload: { ques: data.data.data } });
      setUserQuesLoading("end");
    } catch (error) {
      console.log("查找问卷模板失败---");
      setUserQuesLoading("error");
      console.log(error);
    }
  };

  const setLoading = (value) => {
    setUserQuesLoading(value);
  };

  return (
    <userQuesContext.Provider
      value={{
        useQuesLoading,
        UserQuesData,
        newQues,
        updateAnswer,
        updateQues,
        getQuesTemplate,
        setLoading,
      }}
    >
      {children}
    </userQuesContext.Provider>
  );
};

export default userQuesContext;

// const editAnswer = async (key1, key2, key3, value) => {
//   // console.log("答案---", value);
//   await setQuesBody((preQuesBody) => {
//     return preQuesBody.map((item1, index1) => {
//       let t_contents = {};
//       // if (index1 == key1) {
//       t_contents = item1.contents.map((item2, index2) => {
//         // console.log("题目---", item2);
//         if (index1 == key1 && index2 == key2 && item2.type != 5) {
//           // console.log("保存答案---", item2.skipType);
//           // 如果存在跳过题目的可能
//           if (item2.skipType != 0) {
//             for (let i = 0; i < item2.skipDetail.length; i++) {
//               let isSkipValue;
//               isSkipValue = isSkip(
//                 item2.skipType,
//                 item2.type,
//                 value.answer,
//                 item2.skipDetail[i],
//                 item2.relation,
//                 [key1, key2, key3]
//               );
//               // console.log(
//               //   "是否跳过---",
//               //   isSkipValue,
//               //   item2.skipDetail[i].skipNumber
//               // );
//               handleSkip(!isSkipValue, item2.skipDetail[i].skipNumber);
//             }
//           }
//           return {
//             ...item2,
//             optionFlag: value.optionFlag,
//             answer: value.answer,
//           };
//         } else if (index1 == key1 && index2 == key2 && item2.type == 5) {
//           console.log("复合题型");
//           if (value.title[key3].skipType != 0) {
//             for (let i = 0; i < value.title[key3].skipDetail.length; i++) {
//               let isSkipValue;
//               isSkipValue = isSkip(
//                 value.title[key3].skipType,
//                 value.title[key3].type,
//                 value.title[key3].answer,
//                 value.title[key3].skipDetail[i],
//                 [],
//                 [key1, key2, key3]
//               );
//               console.log(
//                 "是否跳过---",
//                 isSkipValue,
//                 value.title[key3].skipDetail[i].skipNumber
//               );
//               handleSkip(
//                 !isSkipValue,
//                 value.title[key3].skipDetail[i].skipNumber
//               );
//             }
//           }
//           return value;
//         } else {
//           return { ...item2 };
//         }
//       });
//       return {
//         ...item1,
//         contents: t_contents,
//       };
//     });
//   });
//   console.log("有答案啦---", QuesBody);
// };
